<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
{#    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css" rel="stylesheet">#}
{#    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.11.1/font/bootstrap-icons.min.css" rel="stylesheet">#}
{#    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>#}
{#    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/js/bootstrap.bundle.min.js"></script>#}

    <link href="{{ url_for('static',filename='bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static',filename='bootstrap/bootstrap-icons-1.11.1/bootstrap-icons.min.css') }}" rel="stylesheet">
    <script src="{{ url_for('static',filename='jquery/jquery-3.7.1.min.js') }}"></script>
    <script src="{{ url_for('static',filename='bootstrap/js/bootstrap.bundle.min.js') }}"></script>
    <style>
        :root {
            --primary-color: #2E7D32;
            --secondary-color: #81C784;
            --accent-color: #DCEDC8;
            --text-color: #212121;
            --light-text: #757575;
        }

        body {
            background-color: #F5F5F5;
            background-size: cover;
            background-position: center;
            background-blend-mode: overlay;
            min-height: 100vh;
            color: var(--text-color);
            display: flex;
            flex-direction: column;
            margin: 0;
            padding: 0;
        }

        .header-container {
            width: 100%;
            background: linear-gradient(90deg, #1B5E20, #2E7D32, #388E3C);
            box-shadow: 0 3px 15px rgba(0, 0, 0, 0.3);
            padding: 0;
            margin: 0;
            position: relative;
            z-index: 100;
            text-align: center;
        }

        .header-content {
            max-width: 1400px;
            margin: 0 auto;
            padding: 1.2rem 2rem;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }

        .title-container {
            display: flex;
            align-items: center;
            margin-bottom: 0.5rem;
        }

        .header-logo {
            height: 90px;
            width: 90px;
            margin-right: 1.5rem;
            background-color: rgba(255, 255, 255, 0.95);
            padding: 5px;
            border-radius: 50%;
            object-fit: contain;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
        }

        .header-title {
            color: #FFFFFF;
            font-size: 2.2rem;
            font-weight: 700;
            margin: 0;
            text-shadow: 2px 2px 3px rgba(0, 0, 0, 0.4);
            letter-spacing: 1px;
        }

        .header-slogan {
            color: #E8F5E9;
            font-size: 1.3rem;
            font-weight: 400;
            margin: 0;
            letter-spacing: 2px;
            text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
        }

        .main-container {
            width: 100%;
            max-width: 800px;
            margin: 2rem auto;
            padding: 0 2rem;
            flex-grow: 1;
            display: flex;
            align-items: center;
        }

        .content-box {
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 2rem;
            width: 100%;
        }

        .register-container {
            display: flex;
            flex-direction: column;
            /* 调整表单宽度为原来的85% */
            width: 85%;
            margin: 0 auto;
        }

        .logo-container {
            text-align: center;
            margin-bottom: 1rem;
        }

        .logo {
            width: 80px;
            height: 80px;
            object-fit: contain;
        }

        h1 {
            color: var(--primary-color);
            font-weight: 700;
            margin-bottom: 1.5rem;
            text-align: center;
            font-size: 1.8rem;
        }

        .form-control:focus {
            border-color: var(--secondary-color);
            box-shadow: 0 0 0 0.25rem rgba(129, 199, 132, 0.25);
        }

        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            width: 100%;
            padding: 0.5rem;
            font-weight: 600;
            margin-top: 0.75rem;
            transition: all 0.3s ease;
        }

        .btn-primary:hover, .btn-primary:focus {
            background-color: #1B5E20;
            border-color: #1B5E20;
            transform: translateY(-2px);
        }

        .btn-secondary {
            background-color: var(--secondary-color);
            border-color: var(--secondary-color);
            color: white;
            padding: 0.5rem;
            font-weight: 600;
            transition: all 0.3s ease;
        }

        .btn-secondary:hover, .btn-secondary:focus {
            background-color: #66BB6A;
            border-color: #66BB6A;
        }

        .register-footer {
            margin-top: 0.75rem;
            text-align: center;
            font-size: 0.9rem;
            color: var(--light-text);
        }

        .login-link {
            margin-top: 1.5rem;
            text-align: center;
        }

        .login-link a {
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 500;
        }

        .login-link a:hover {
            text-decoration: underline;
        }

        .contact-info {
            margin-top: 1.5rem;
            text-align: center;
            font-size: 0.8rem;
            color: var(--light-text);
            border-top: 1px solid #eee;
            padding-top: 0.75rem;
        }

        /* 确保所有图标容器宽度一致 */
        .input-group-text {
            background-color: var(--accent-color);
            border-color: #ced4da;
            color: var(--primary-color);
            min-width: 40px;  /* 设置最小宽度确保一致性 */
            justify-content: center;  /* 确保图标居中 */
        }

        .animated-bg {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            background: linear-gradient(120deg, #e0f7fa, #e8f5e9, #f1f8e9);
            background-size: 300% 300%;
            animation: gradientShift 15s ease infinite;
        }

        @keyframes gradientShift {
            0% {background-position: 0% 50%;}
            50% {background-position: 100% 50%;}
            100% {background-position: 0% 50%;}
        }

        /* 修改验证码组的样式 */
        .verification-group {
            display: flex;
            /* 移除这里的gap */
        }

        /* 确保图标和输入框无缝连接 */
        .verification-group .input-group-text {
            border-right: none;
        }

        .verification-group .form-control {
            flex-grow: 1;
            border-left: none;
            border-top-left-radius: 0;
            border-bottom-left-radius: 0;
        }

        /* 为按钮添加左边距，而不是整个组的gap */
        .verification-group .btn {
            width: auto;
            flex-shrink: 0;
            margin-left: 10px;
        }

        @media (max-width: 767.98px) {
            .header-title {
                font-size: 1.6rem;
            }

            .header-slogan {
                font-size: 1rem;
                letter-spacing: 1px;
            }

            .title-container {
                flex-direction: column;
                margin-bottom: 0.5rem;
            }

            .header-logo {
                margin-right: 0;
                margin-bottom: 0.5rem;
                height: 50px;
                width: 50px;
            }

            .main-container {
                margin-top: 1rem;
            }

            .verification-group {
                flex-direction: column;
            }

            .verification-group .input-group {
                width: 100%;
            }

            .verification-group .btn {
                width: 100%;
                margin-left: 0;
                margin-top: 5px;
            }
        }
    </style>
</head>
<body>
    <div class="animated-bg"></div>

    <!-- 标题栏 -->
    <header class="header-container">
        <div class="header-content">
            <div class="title-container">
                <img src="{{ url_for('static', filename='images/logo_1.png') }}" alt="林业3PG模型系统" class="header-logo">
                <h1 class="header-title">林序3PG模型可视化系统</h1>
            </div>
            <p class="header-slogan">精算生态&nbsp;&nbsp;智绘林颜</p>
        </div>
    </header>

    <div class="container main-container">
        <div class="content-box">
            <div class="register-container">
                <h1>用户注册</h1>

                <form id="registerForm">
                    <!-- 邮箱 -->
                    <div class="mb-3">
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-envelope-fill"></i></span>
                            <input type="email" class="form-control" id="email" placeholder="请输入邮箱" required>
                        </div>
                    </div>

                    <!-- 验证码 - 修改结构确保紧贴图标 -->
                    <div class="mb-3">
                        <div class="verification-group">
                            <!-- 图标和输入框作为一个输入组 -->
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-shield-lock-fill"></i></span>
                                <input type="text" class="form-control" id="verificationCode" placeholder="请输入验证码" required>
                            </div>
                            <!-- 按钮单独放置，通过CSS控制间距 -->
                            <button type="button" class="btn btn-secondary" id="sendCodeBtn">发送验证码</button>
                        </div>
                    </div>

                    <!-- 用户名 -->
                    <div class="mb-3">
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-person-fill"></i></span>
                            <input type="text" class="form-control" id="username" placeholder="请输入用户名" required>
                        </div>
                    </div>

                    <!-- 密码 -->
                    <div class="mb-3">
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-lock-fill"></i></span>
                            <input type="password" class="form-control" id="password" placeholder="请输入密码" required>
                        </div>
                    </div>

                    <!-- 确认密码 -->
                    <div class="mb-3">
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-key-fill"></i></span>
                            <input type="password" class="form-control" id="confirmPassword" placeholder="请确认密码" required>
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary">注 册</button>
                </form>

                <div class="login-link">
                    <p class="mb-0">已有账号？<a href="{{ url_for('auth.login') }}">立即登录</a></p>
                </div>

                <div class="register-footer">
                    <p class="mb-0">3PG模型是一种基于生理机理的森林生长预测工具。</p>
                </div>

                <div class="contact-info">
                    <p class="mb-1">联系我们</p>
                    <p class="mb-1">邮箱：finmzhao@163.com</p>
                    <p class="mb-1">电话：+86 185 1003 0270</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        /**
         * 显示简单提示信息
         * @param {string} message - 提示内容
         * @param {string} type - 类型：'success'(正确), 'warning'(警告), 'error'(错误)
         * @param {number} duration - 显示时间（毫秒）
         */
        function showMessage(message, type = 'success', duration = 2000) {
          // 设置颜色和图标
          let color, icon;
          switch(type) {
            case 'success':
              color = '#4CAF50';
              icon = '✓';
              break;
            case 'warning':
              color = '#FF9800';
              icon = '⚠';
              break;
            case 'error':
              color = '#F44336';
              icon = '✗';
              break;
            default:
              color = '#4CAF50';
              icon = '✓';
          }

          // 创建toast元素
          const toast = $(`
            <div style="
              margin-bottom: 10px;
              padding: 10px 15px;
              background-color: ${color};
              color: white;
              border-radius: 4px;
              box-shadow: 0 2px 5px rgba(0,0,0,0.2);
              display: flex;
              align-items: center;
              opacity: 0;
              transition: opacity 0.3s ease;
            ">
              <span style="margin-right: 10px; font-weight: bold;">${icon}</span>
              <span>${message}</span>
            </div>
          `).appendTo('#simple-toast');

          // 显示元素
          setTimeout(() => toast.css('opacity', 1), 10);

          // 设置自动隐藏
          setTimeout(() => {
            toast.css('opacity', 0);
            setTimeout(() => toast.remove(), 300);
          }, duration);
        }

        $(document).ready(function() {
            if (!$('#simple-toast').length) {
                $('body').append('<div id="simple-toast" style="position:fixed; top:24%; left:50%; transform:translate(-50%, -24%); z-index:9999;"></div>');
            }

            // 发送验证码按钮点击事件
            $("#sendCodeBtn").click(function() {
                const email = $("#email").val();

                if (!email) {
                    showMessage("请先输入邮箱地址", "warning");
                    return;
                }

                // 验证邮箱格式
                const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
                if (!emailRegex.test(email)) {
                    showMessage("请输入有效的邮箱地址", "error");
                    return;
                }

                // 禁用按钮并开始倒计时
                const btn = $(this);
                btn.prop("disabled", true);
                let countdown = 30;
                btn.text(`${countdown}秒后重新发送`);

                const timer = setInterval(function() {
                    countdown--;
                    btn.text(`${countdown}秒后重新发送`);

                    if (countdown <= 0) {
                        clearInterval(timer);
                        btn.prop("disabled", false);
                        btn.text("发送验证码");
                    }
                }, 1000);

                // 这里添加发送验证码的AJAX请求
                $.ajax({
                    url: "/captcha/email?email="+email,
                    type: "GET",
                    success: function(response) {
                        if (response.success) {
                            showMessage("验证码已发送至您的邮箱", "success");
                        } else {
                            showMessage(response.message, "error");
                            // 如果发送失败，重置按钮
                            clearInterval(timer);
                            btn.prop("disabled", false);
                            btn.text("发送验证码");
                        }
                    },
                    error: function(xhr, status, error) {
                        showMessage("服务出错：" + error, "error");
                        // 如果发送失败，重置按钮
                        clearInterval(timer);
                        btn.prop("disabled", false);
                        btn.text("发送验证码");
                    }
                });
            });

            // 注册表单提交
            $("#registerForm").submit(function(e) {
                e.preventDefault();

                // 获取表单数据
                const email = $("#email").val();
                const verificationCode = $("#verificationCode").val();
                const username = $("#username").val();
                const password = $("#password").val();
                const confirmPassword = $("#confirmPassword").val();

                // 进行表单验证
                if (!email || !verificationCode || !username || !password || !confirmPassword) {
                    showMessage("请填写完整注册信息", "warning");
                    return;
                }

                // 验证两次密码是否一致
                if (password !== confirmPassword) {
                    showMessage("两次输入的密码不一致", "error");
                    return;
                }

                // 发送注册请求
                $.ajax({
                    url: '{{ url_for("auth.register") }}',
                    type: "POST",
                    contentType: 'application/json',
                    data: JSON.stringify({
                        email: email,
                        captcha: verificationCode,
                        username: username,
                        password: password,
                        password_confirm: confirmPassword
                    }),
                    success: function(response) {
                        if (response.success) {
                            showMessage(response.message, "success");
                            // 注册成功后延迟跳转到登录页面
                            setTimeout(function() {
                                window.location.href = "{{ url_for('auth.login') }}";
                            }, 1300);
                        } else {
                            showMessage(response.message, "error");
                        }
                    },
                    error: function(xhr, status, error) {
                        showMessage("服务出错：" + error, "error");
                    }
                });
            });
        });
    </script>
</body>
</html>